package com.xkstream.client.common;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.xkstream.client.R;
import com.xkstream.client.presenter.LiveRoomContract;

import butterknife.OnClick;

/**
 * Created by xiaoyi on 2017-10-11.
 */

public class CommonDialog extends Dialog {
    private View contentView;
    private TextView tvTitle, tvLeft, tvRight;

    public CommonDialog(Context context) {
        super(context, R.style.CommonDialog);
        init(context);
    }

    public CommonDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public CommonDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onStart() {
        super.onStart();
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    private void init(Context context) {
        Window window = getWindow();
        WindowManager.LayoutParams params =
                window.getAttributes();
        params.width = (int) (((WindowManager)
                context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth()
                / 10f * 8);
        window.setGravity(Gravity.CENTER);
        window.setAttributes(params);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        contentView = View.inflate(context, R.layout.layout_common_dialog, null);
        window.setContentView(contentView);

        findTitle();
    }

    private void findTitle() {
        tvTitle = (TextView) contentView.findViewById(R.id.tvTitle);
        tvLeft = (TextView) contentView.findViewById(R.id.tvCancel);
        tvRight = (TextView) contentView.findViewById(R.id.tvSure);

        tvLeft.setOnClickListener((v) -> dismiss());
    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public void setListener(Listener listener) {
        this.listener = listener;
        tvRight.setOnClickListener((v) -> {
            if (listener != null) {
                listener.onClick();
                dismiss();
            }
        });

    }



    public void setLeftListener(LeftListener listener){
        this.leftListener = listener;
        tvLeft.setOnClickListener((v)->{
            if (leftListener!=null){
                leftListener.onClick();
            }
        });
    }

    public void setLeftText(String text) {
        tvLeft.setText(text);
    }

    public void setRightText(String text) {
        tvRight.setText(text);
    }

    public interface Listener {
        void onClick();
    }

    public interface LeftListener{
        void onClick();
    }

    private Listener listener;
    private LeftListener leftListener;

    public void setSingleButton() {
        tvLeft.setVisibility(View.GONE);
        findViewById(R.id.line).setVisibility(View.GONE);
    }


}
